﻿using System;
using PmSadis.Repositories.Abstract;
using System.Linq;
using DevExpress.Web.Data;
using PmSadis.WebFormsUi.ExpandedControls;
using DevExpress.Web.ASPxGridView;

namespace PmSadis.WebFormsUi.Operator
{
    public partial class Units : System.Web.UI.Page
    {
        private IListRepository<PmSadis.Models.Unit> _repo = new Repositories.UnitRepository();
        
        protected void gvUnits_Init(object sender, EventArgs e)
        {
            gvUnits.SetDefaultSettings();
        }
        
        public IQueryable<PmSadis.Models.Unit> GetAll()
        {
            return _repo.All;
        }
        public void DeleteUnit(int unitId)
        {
            _repo.Delete(unitId);
        }
        public int InsertUnit(string name, string fullName, byte decimalPlace)
        {
            var unit=new PmSadis.Models.Unit{
                UnitId=0,
                Name=name,
                FullName=fullName,
                DecimalPlace=decimalPlace
            };
            return (int)_repo.Edit(unit);
        }
        public int UpdateUnit(int unitId, DateTime dateCreate, string name, string fullName, byte decimalPlace)
        {
            var unit = new PmSadis.Models.Unit
            {
                UnitId = unitId,
                DateCreate=dateCreate,
                Name = name,
                FullName = fullName,
                DecimalPlace = decimalPlace
            };
            return (int)_repo.Edit(unit);
        }
        
        protected void gvUnitsRowUpdating(object sender, ASPxDataUpdatingEventArgs e)
        {
            var grid = sender as ASPxGridView;
            var dateCreate = grid.GetRowValues(gvUnits.EditingRowVisibleIndex, "DateCreate");
            e.NewValues["DateCreate"] = dateCreate;
        }
    }
}